package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;

import cn.exrick.xboot.modules.research.entity.Achievement;
import cn.exrick.xboot.modules.research.entity.AwardManagement;
import cn.exrick.xboot.modules.research.service.IAwardManagementService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "科研获奖管理管理接口")
@RequestMapping("/xboot/awardManagement")
@Transactional
public class AwardManagementController {

    @Autowired
    private IAwardManagementService iAwardManagementService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<AwardManagement> get(@PathVariable String id) {

        AwardManagement awardManagement = iAwardManagementService.getById(id);
        return new ResultUtil<AwardManagement>().setData(awardManagement);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<AwardManagement>> getAll() {

        List<AwardManagement> list = iAwardManagementService.list();
        return new ResultUtil<List<AwardManagement>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<AwardManagement>> getByPage(@ModelAttribute AwardManagement awardManagement,@ModelAttribute PageVo page) {
        QueryWrapper<AwardManagement> qw =new QueryWrapper<>();

        if (StrUtil.isNotBlank(awardManagement.getAwardId())){
             qw.like("award_Id",awardManagement.getAwardId());
        }
        if (StrUtil.isNotBlank(awardManagement.getAwardName())){
            qw.like("award_Name",awardManagement.getAwardName());
        }
        if (StrUtil.isNotBlank(awardManagement.getAwardee())){
            qw.like("awardee",awardManagement.getAwardee());
        }
        if (StrUtil.isNotBlank(awardManagement.getDepartmentName())){
            qw.like("department_name", awardManagement.getDepartmentName());
        }
        if (StrUtil.isNotBlank(awardManagement.getUnit())){
            qw.like("unit",awardManagement.getUnit());
        }
        if (StrUtil.isNotBlank(awardManagement.getTime())){
            qw.like("time",awardManagement.getTime());
        }

        IPage<AwardManagement> data = iAwardManagementService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<AwardManagement>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<AwardManagement> saveOrUpdate(AwardManagement awardManagement) {

        if (iAwardManagementService.saveOrUpdate(awardManagement)) {
            return new ResultUtil<AwardManagement>().setData(awardManagement);
        }
        return new ResultUtil<AwardManagement>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iAwardManagementService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/importDataApi",method = RequestMethod.POST)
    @ApiOperation(value = "导入数据")
    public Result<Object> importData(@RequestBody List<AwardManagement> awardManagements){

        List<Integer> errors = new ArrayList<>();
        List<String> reasons = new ArrayList<>();
        int count = 0;
        for (AwardManagement awardManagement : awardManagements) {
            count++;
            if(StrUtil.isBlank(awardManagement.getAwardId())||StrUtil.isBlank(awardManagement.getAwardName())
                    ||StrUtil.isBlank(awardManagement.getAwardee())){
                errors.add(count);
                reasons.add("有字段为空");
                continue;
            }

            iAwardManagementService.saveOrUpdate(awardManagement);
        }


        // 批量保存数据
        int successCount = awardManagements.size() - errors.size();
        String successMessage = "全部导入成功，共计 " + successCount + " 条数据";
        String failMessage = "导入成功 " + successCount + " 条，失败 " + errors.size() + " 条数据。<br>" +
                "第 " + errors.toString() + " 行数据导入出错，错误原因分别为：<br>" + reasons.toString();
        String message = "";
        if(errors.size()==0){
            message = successMessage;
        }else{
            message = failMessage;
        }
        return ResultUtil.success(message);
    }
}
